﻿using sqlsearcher.Models.Extends;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sqlsearcher.Models
{
    public class SqlTextColor
    {
        public static string[] chars3 = new string[] { "[", "]", "{", "}" };

        public static void SetColor(MyRichTextBox txt,List<string> sqlKeys,List<string> dbKeys)
        {
            if (sqlKeys.Count > 0)
                txt.SetColorWord(sqlKeys, Color.Blue, false);
            if (dbKeys.Count > 0)
                txt.SetColorWord(dbKeys, Color.Green, false);
            txt.RunColor();
            //txt.SuspendDrawing();
            //var selectIndex = txt.SelectionStart;
            //foreach (var item in sqlKeys)
            //{
            //    SetColor(txt, item.ToLower(), Color.Blue);
            //    SetColor(txt, item.ToUpper(), Color.Blue);
            //    SetColor(txt, item, Color.Blue);
            //}
            //foreach (var item in dbKeys)
            //{
            //    SetColor(txt, item, Color.Green);
            //    SetColor(txt, item.ToLower(), Color.Green);
            //    SetColor(txt, item.ToUpper(), Color.Green);
            //}
            //foreach (var item in chars3)
            //{
            //    SetColor(txt, item, Color.Blue, true);
            //}
            //txt.Select(selectIndex, 0);
            //txt.ResumeDrawing();
           


        }
        private static void SetColor(RichTextBox txt, string key, Color color, bool isBold = false)
        {
            ArrayList list = getIndexArray(txt.Text, key);
            for (int i = 0; i < list.Count; i++)
            {
                int index = (int)list[i];
                txt.Select(index, key.Length);
                txt.SelectionColor = color;
            }

        }
        private static ArrayList getIndexArray(String inputStr, String findStr)
        {
            ArrayList list = new ArrayList();
            int start = 0;
            while (start < inputStr.Length)
            {
                int index = inputStr.IndexOf(findStr, start);
                if (index >= 0)
                {
                    list.Add(index);
                    start = index + findStr.Length;
                }
                else
                {
                    break;
                }
            }
            return list;

        }
    }
}
